package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.CommResponseBean;
import com.shenzhen.horselive.beans.UserAcountBean;
import com.shenzhen.horselive.databinding.ActivityFenZhiTransferOutBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.presenter.ViewObserver;

import java.text.NumberFormat;

/**
 * 峰值转出或者红包股转出
 */
public class FenZhiTransferOutActivity extends AppBarActivity {

    ActivityFenZhiTransferOutBinding  binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFenZhiTransferOutBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleDark();
        setTitle("蜂值转出");
        initViews();
        initData();
    }

    int  type;
    private void initViews() {
        type = getIntent().getIntExtra("type",1);
        switch (type)
        {
            case 1:
                //峰值转出
                setTitle("蜂值转出");
                binding.etInput.setHint("请输入转出蜂值数量");
                binding.tvDanwei.setText("个");
                break;
            case 2:
                //红包股转出
                setTitle("红包股转出");
                binding.etInput.setHint("请输入转出红包股数量");
                binding.tvDanwei.setText("股");
                break;
        }

        binding.tvCashAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //全部转出
                if (bean!=null)
                {
                    NumberFormat  format =NumberFormat.getInstance();
                    format.setMaximumFractionDigits(10);
                    format.setGroupingUsed(false);
                    String  amount  =  format.format(Double.parseDouble(bean.getData().getAmount()));
                    if (type==2)
                    {
                        //红包股转出只能取整
                        int  gu = (int) Double.parseDouble(amount);
                        amount = gu+"";
                    }
                    binding.etInput.setText(amount);
                    binding.etInput.setSelection(amount.length());
                }
            }
        });

        binding.btGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });

    }


    private void submit() {
        String  num = binding.etInput.getText().toString();
        if (TextUtils.isEmpty(num))
        {
            return;
        }
        //换算对应的数字
        if (bean==null)
            return;
        String  targetNum  = ""+ Double.parseDouble(num)*Double.parseDouble(bean.getData().getRatio());
        ApiFactory.getApiService().fenDuiRedGu(User.get().getMyInfo().getToken()
                ,type+"","0",targetNum+"")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<CommResponseBean>(this) {
                    @Override
                    protected void onNextInActive(CommResponseBean commResponseBean) {
                        if (commResponseBean.isSuccess())
                        {
                            showToast("转出成功");
                            finish();
                        }
                        else
                        {
                            showToast(commResponseBean.getErrMessage());
                        }
                    }
                });

    }


    private void initData() {
      getAcountForType(type);
    }

    UserAcountBean  bean;
    //帐户类型: 0:人民币帐户 ,1:蜂值帐户 ,2:红包股帐户 ,3:金卡帐户
    private void getAcountForType(int i) {
        ApiFactory.getApiService().accountTypes(User.get().getMyInfo().getToken(),i+"")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<UserAcountBean>(this) {
                    @Override
                    protected void onNextInActive(UserAcountBean userAcountBean) {
                        if (userAcountBean.isSuccess())
                        {
                            bean  =userAcountBean;
                            if (type==1)
                            {
                                binding.tvNotice.setText(String.format("可转出蜂值：%s个",formatNumber(userAcountBean.getData().getAmount())));
                            }else if (type==2)
                            {
                                binding.etInput.setInputType(InputType.TYPE_CLASS_NUMBER);
                                binding.tvNotice.setText(String.format("可转出红包股：%s股",formatNumber(userAcountBean.getData().getAmount())));
                            }
                        }
                    }
                });

    }


    /**
     * 1:蜂值,2:红包股
     * @param context
     * @param type
     */
    public static void start(Context context,int type) {
        Intent starter = new Intent(context, FenZhiTransferOutActivity.class);
        starter.putExtra("type",type);
        context.startActivity(starter);
    }


    /**
     * 保留十位有效的数字
     * @param d
     * @return
     */
    private   String  formatNumber(String  d)
    {
        NumberFormat  format = NumberFormat.getInstance();
        format.setMaximumFractionDigits(10);
        format.setGroupingUsed(false);
        return format.format(Double.parseDouble(d));
    }


}